//
//  InternetCollectionViewCell.m
//  SnailPost
//
//  Created by Q on 16/3/12.
//  Copyright © 2016年 抠泥ser. All rights reserved.
//

#import "InternetCollectionViewCell.h"
#import "HeaderTableViewCell.h"
#import "NormalTableViewCell.h"
#import "NewsModel.h"
#import "LHAFNetworkTool.h"
#import <UIImageView+WebCache.h>
#import "LMTTImeChuo.h"
#import <MJRefresh.h>
#define CHEIGHT self.contentView.frame.size.height
#define CWIDTH self.contentView.frame.size.width

@interface InternetCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)NewsModel *model;

@end

@implementation InternetCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
        [self.contentView addSubview:_tableView];
    }
    [self dataHandeler];
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.contentInset = UIEdgeInsetsMake(94, 0, 59, 0);
    
    [_tableView registerClass:[HeaderTableViewCell class] forCellReuseIdentifier:@"headerCell"];
    
    [_tableView registerClass:[NormalTableViewCell class] forCellReuseIdentifier:@"normalCell"];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dataHandeler];
            [_tableView.mj_header endRefreshing];
        });
    }];
    // 设置刷新控件
    _tableView.mj_header = header;
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self loadingData];
        [_tableView.mj_footer endRefreshing];
    }];
    _tableView.mj_footer = footer;
}

- (void)dataHandeler
{
    NSString *urlStr = @"https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=all&pagingAction=up";
    [LHAFNetworkTool getUrl:urlStr body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        self.dataArr = [NewsModel modelHanderWithArray:resposeObject[@"data"][@"data"]];
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

#pragma mark - 加载
- (void)loadingData
{
    _model = _dataArr.lastObject;
    NSString *str = [NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/news?pageSize=20&lastId=%@&columnId=all&pagingAction=down", _model.feedId];
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray *loadArr =[NewsModel modelHanderWithArray:resposeObject[@"data"][@"data"]];
        for (NSInteger i = 0; i < loadArr.count; i++) {
            [_dataArr addObject:loadArr[i]];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    [_tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *n = _dataArr[indexPath.row];
    if (indexPath.row == 0) {
        HeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headerCell" forIndexPath:indexPath];
        [cell.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:n.featureImg]];
        cell.titleLabel.text = n.title;
        return cell;
    } else {
        NormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
        [cell.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:n.featureImg]];
        cell.titleLabel.text = n.title;
        cell.titleLabel.font = [UIFont systemFontOfSize:14];
        cell.titleLabel.numberOfLines = 0;
        cell.digestLabel.text = n.digest;
        cell.ptimeLabel.text = [LMTTImeChuo compareCurrentTimeW:n.publishTime];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return CHEIGHT / 3;
    } else {
        return CHEIGHT / 6;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel *n = _dataArr[indexPath.row];
    [self.delegate InternetValue:n.feedId: n.title];
}


@end
